package com.pixiv.servlet.LoginServlet;

import com.pixiv.MysqlDAO.UserLoginDAO;
import com.pixiv.tools.SendPrintWriterPacket;
import com.pixiv.user.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;

@WebServlet("/LoginServlet")
public class LoginServlet  extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        String type = req.getParameter("appType");
        try {
            Class<?> c = this.getClass();
            Method me = c.getMethod("Service_" + type, HttpServletRequest.class, HttpServletResponse.class);
            me.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //发送登记包
    public void Service_SendLoginPacket(HttpServletRequest req, HttpServletResponse resp)
    {
        resp.setContentType("application/json;charset=utf-8");
        String userName = req.getParameter("User");
        String PWD = req.getParameter("PWD");

        User u = UserLoginDAO.getUserByLoginParameter(userName,PWD);

        if (u != null)
        {
            HttpSession hs = req.getSession();
            hs.setAttribute("GLOBAL_USER_OBJECT",u);
            SendPrintWriterPacket.sendFlag(resp,1);

        }else{
            SendPrintWriterPacket.sendFlag(resp,0);
        }
        //System.out.println(u);

    }


    //跳入登录页面之前的执行事件
    public void Service_InitGotoLogin(HttpServletRequest req, HttpServletResponse resp)
    {
        resp.setContentType("application/json;charset=utf-8");
        SendPrintWriterPacket.sendFlag(resp,1);

    }
}
